<template>
  <div class="default-main ba-table-box">
    <TableHeader
      :buttons="[
        'refresh',
        'add',
        'edit',
        'remove',
        'comSearch',
        'columnDisplay',
        'upload',
        'export',
        'unfold',
      ]"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
    <Table ref="tableRef" :pagination="false" @action="baTable.onTableAction" />
    <!-- 表单 -->
    <PopupForm
      v-if="baTable.form.operate !== 'upload' && baTable.form.operate"
    />
    <!-- 上传excel文件 -->
    <Upload v-if="baTable.form.operate && baTable.form.operate === 'upload'" />
  </div>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, reactive } from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff, getDictData } from "@/utils/common"; // 导入表格api方法生成器
import { BomManager } from "@/api/backend/index";
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import PopupForm from "./popupForm.vue";
import Upload from "./components/import.vue";
import { useRoute } from "vue-router";
import { useNavTabs } from "@/store/navTabs";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useDictionary } from "@/store/backend/tool/dictionary";
import { useBomM } from "@/store/backend/BomM/bomM";
import { useBomDict } from "@/store/backend/BomM/bomDict";
import { useStoreWarehouseLocation } from "@/store/backend/StoreroomM/Location";
import { useSupplierInfo } from "@/store/backend/Supplier/Info";
import { useSupplierChain } from "@/store/backend/Supplier/Chain";

// import { _post } from "@/utils/axios";
const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const navTabs = useNavTabs();
const dictionary = useDictionary();
const bom = useBomM();
const bomD = useBomDict();
const Location = useStoreWarehouseLocation();
const supplier = useSupplierInfo();
const Chain = useSupplierChain();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能 默认10000个
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + BomManager)
      ? (navTabs as any).pageSizeMap.get(route.path + BomManager)
      : 10000;
  }
  return 10000;
};

const baTable = new baTableClass(
  new baTableApi(BomManager),
  {
    pk: "id", //区域ID
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("id"),
        label: "ID",
        prop: "id",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("code"),
        label: "物料编码",
        prop: "code",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("name"),
        label: "物料名称",
        prop: "name",
        align: "left",
      },
      {
        show: filedShowOff("type"),
        label: "物料类型",
        prop: "type",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("drawNo"),
        label: "图号",
        prop: "drawNo",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("memoryName"),
        label: "库位",
        prop: "memoryName",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("position"),
        label: "仓位",
        prop: "position",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("perUsage"),
        label: "每用量",
        prop: "perUsage",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("workshopCode"),
        label: "车间代码",
        prop: "workshopCode",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("workshopName"),
        label: "车间名称",
        prop: "workshopName",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: operatorShowOff([
          route.path + "/edit",
          route.path + "/add",
          route.path + "/remove",
        ]),
        label: "操作",
        align: "center",
        width: "130",
        operator: false,
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "itemAddButton",
            name: "itemAdd", //与add绑定
            title: "新增",
            text: "",
            type: "primary",
            icon: "iconfont icon-xinzeng",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      pageNum: 1,
      pageSize: pageSizeKeep(),
    },
    extend: {
      maxHeight: 580,
    },
  },
  {
    defaultItems: {},
  }
);

//获取结算方式/开票方式字典
const getMenuStatus = () => {
  //采购单结算方式
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["settlementMethod"])
  ) {
    dictionary.getDictInfo("sys_purchase_settlement").then((res: any) => {
      dictionary.moreTags["settlementMethod"] = getDictData(res);
      dictionary.moreTags["settlementMethodOrigin"] = res;
    });
  }
  //开票方式
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["billingMethod"])
  ) {
    dictionary.getDictInfo("sys_purchase_billing").then((res: any) => {
      dictionary.moreTags["billingMethod"] = getDictData(res);
      dictionary.moreTags["billingMethodOrigin"] = res;
    });
  }
};
//获取结算方式/开票方式字典
getMenuStatus();

onMounted(() => {
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(BomManager);
});
// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("dictionary", dictionary);
provide("bom", bom);
provide("bomD", bomD);
provide("Location", Location);
provide("supplier", supplier);
provide("Chain", Chain);
</script>
<style lang="scss" scoped>
.default-main {
  margin: 0px !important;
}
</style>
